package com.codeyang.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 描述: 系统用户登录类 集成security的 UserDetails
 *
 * @author CodeYang_Site
 * @version 2021/5/22 20:24
 */
@ApiModel(value = "com-codeyang-domain-SysUser")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class SysLoginUser implements UserDetails {

    @TableId(value = "user_id", type = IdType.INPUT)
    @ApiModelProperty(value = "")
    private Long userId;

    /**
     * 用户名
     */
    @TableField(value = "username")
    @ApiModelProperty(value = "用户名")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "`password`")
    @ApiModelProperty(value = "密码")
    private String password;

    @TableField(exist = false)
    @ApiModelProperty(value = "后台权限集合")
    private List<String> auths;


    /**
     * 状态  0：禁用   1：正常
     */
    @TableField(value = "`status`")
    @ApiModelProperty(value = "状态  0：禁用   1：正常")
    private Byte status;


    /**
     * 权限的相关操作
     * 根据表的设计数据
     * sys:user:page,sys:user:info 需要分割一下
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        //判断auths是否已经 放入了数据
        if (CollectionUtils.isEmpty(auths)) {
            return grantedAuthorities;
        }
        //不为空就遍历数据记录
        auths.forEach(auth -> {
            if (StringUtils.hasText(auth)) {
                //分割逗号 sys:user:page,sys:user:info
                String[] finalAuths = auth.split(",");
                for (String finalAuth : finalAuths) {
                    grantedAuthorities.add(new SimpleGrantedAuthority(finalAuth));
                }

            }
        });

        return grantedAuthorities;
    }

    /**
     * 用户名不常用
     * 用id
     *
     * @return
     */
    @Override
    public String getUsername() {
        return String.valueOf(this.userId);
    }
//    public String getUsername() {
//        return String.valueOf(this.userId);
//    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}
